/**
 * create by 9-3-17
 * 
 */

const addressModalController = require('./address.modal.controller')
addressController.$inject = ['$scope', '$location', 'addressService', '$uibModal'];

function addressController($scope, $location, addressService, $modal) {
    var ac = this;

    var addressInfo={
        "_id":"",
        "address":"",
        "name":"",
        "phone":""
    };
    ac.addressInfo={};
    ac.addressList=[];
    $scope.selectedAddr = '';

    // vm.addressInfo.address
    // vm.addressInfo.name
    // vm.addressInfo.phone

    ac.getAllAddressList=function(){
        let customer = JSON.parse(sessionStorage.getItem("customer"));
         addressService.getAllAddressList(customer.id,function (error, result) {
            if (error) {
                // winston.error("error"+error.message);
                console.log("error" + error.message);
            }
            else {
                // winston.info("success to call back" + result.uname);
                console.log("success to call back");
                console.log(result);
                ac.addressList = result;
            }

        });
    }

    ac.openModal = function () {
        var modalInstance = $modal.open({
            template: require('../layouts/address.modal.html'),
            controller: addressModalController,
             resolve: {
                addressInfo: function () {
                    // get the id from session
                    let customer=JSON.parse(sessionStorage.getItem("customer"));
                    console.log(customer);
                    ac.addressInfo={};
                    ac.addressInfo._id=customer.id;
                    return ac.addressInfo;
                },
                getAllAddressList:function(){
                    return ac.getAllAddressList;
                }
            }
        });
        modalInstance.opened.then(function () {//when the modal open
            console.log('modal is opened');
        });
        modalInstance.result.then(function (result) {
            console.log(result);
        }, function (reason) {
            console.log(reason);
        });
    };


    ac.getSelectedAddr = function(){
        console.log("当前选择的是 "+ $scope.selectedAddr);
        sessionStorage.setItem('selectedAddr', $scope.selectedAddr);
    }

    ac.getAllAddressList();

}

module.exports = addressController;